<?php

namespace app\api\model;

class Product extends BaseModel
{
    public function getMainImgUrlAttr($value,$data){
        return $this->prefixImgUrl($value,$data);
    }

    public function imgs(){
        //一对多,必须return返回
       return  $this->hasMany('ProductImage','product_id','id');
    }

    public function properties(){
       return  $this->hasMany('ProductProperty','product_id','id');
    }

    public static function getMostRecent($count){
        //查询指定数量数据并且按创建时间倒叙排序
        $products=self::limit($count)->order('create_time desc')->select();
        return $products;
    }

    public static function getProductByCategoryID($category_id){
        $products = self::where('category_id','=',$category_id)->select();
        return $products;
    }

    //获取商品详情
    public static function getProductDetail($id){
        $product = self::with('imgs.imgUrl,properties')->find($id);
        return $product;
    }
    /*//imgUrl不显示所以先暂时不用
     *  public static function getProductDetail($id){
        $product = self::with([
                'imgs' => function($query){
                    $query->with(['imgUrl'])
                     ->order('order','asc');
                }
            ])->with(['properties'])->find($id);
        return $product;
    }
     */
}
